﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModeStudy
{
    /// <summary>
    /// 观察者模式
    /// </summary>
    class Subject
    {
        public delegate void ClickBord(object sender, ClickKey key);//定义一个委托

        public event ClickBord ClickBordEvent;//定义一个委托事件变量

        public string BordColor = "red";

        private string CurrentKey;//被监视的敏感属性
        public class ClickKey:EventArgs      //提供监视对象  需要暴露的属性
        {
            public readonly string BordKey;

            public ClickKey(string Bkey)
            {
                BordKey = Bkey;
            }
        }

        protected virtual void InvokeFunc(ClickKey ckey) //执行委托事件的方法
        {
            if (ClickBordEvent != null)
            {
                ClickBordEvent(this, ckey);
            }
        }

        public void printKey(string keyNames)         //对外暴露的方法
        {
            var click = new ClickKey(keyNames);
            InvokeFunc(click);
        }
    }

    class Observer
    {
        public static void ShowView(object sender,Subject.ClickKey e)
        {
            var keyBord = sender as Subject;
            Console.WriteLine("你得键盘颜色是："+keyBord.BordColor);
            Console.WriteLine("您输入的字母是："+e.BordKey);
        }
    }

    class Test
    {
        public static void Main(string[] args)
        {
            var subject=new Subject();
            subject.ClickBordEvent += Observer.ShowView;
            subject.printKey("G");
            Console.ReadLine();
        }
    }

}
